﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using MvcDeviceManager.Models;
using MvcDeviceManager.Filter;

namespace MvcDeviceManager.Controllers
{
    public class RoleController : Controller
    {
        //
        // GET: /Role/
        [AuthorizeFilter]
        public ActionResult Index()
        {
            string[] roles = Roles.GetAllRoles();
            ViewBag.roles = roles;
            return View();
        }
        [AuthorizeFilter]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [AuthorizeFilter]
        public ActionResult Create(Role role )
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Roles.CreateRole(role.RoleName);
                    return RedirectToAction("Index");
                }catch{
                    ModelState.AddModelError("RoleName", String.Format("对不起，角色[{0}]已经存在!", role.RoleName));
                }
                
            }
            return View(role);
        }
    }
}
